home *** CD-ROM | disk | FTP | other *** search
/ A.C.E. 2 / ACE CD 2.iso / FILES / DOCS / AMOSDOC.LHA / AmosHyperBook.dms / in.adf / Manual / ALLIANCE-11 / ALLIANCE-11
Encoding:
Text File  |  1992-03-20  |  19.9 KB  |  490 lines

  1. 11: HARDWARE SPRITES
  2. One of the biggest attractions of the Commodore Amiga is its ability to
  3. produce high quality games which rivial those found on genuine arcade
  4. machines. This can be amply demonstrated by terrific programs such as
  5. Battle Squadron and Eliminator.
  6.  
  7.   Now, for the first time, all these amazing features are at your
  8. fingertips! AMOS Basic provides you with complete control over the
  9. Amiga's hardware and software sprites. These sprites can be
  10. effortlessly manoeuvred with the built-in AMAL animation language, so
  11. you don't have to be a machine code wizard in order to create your own
  12. stunning arcade games.
  13.  
  14.   Hardware sprites are searate images which can be automatically
  15. overlayed on the Amiga's screen. The classic example of a hardware
  16. sprite is the mouse pointer. This is completely independent of the
  17. screen, and works equally well in all the Amiga's graphics modes.
  18.  
  19.   Since sprites don't interfere with the screen background, they are
  20. perfect for the moving objects required by an arcade game. Not only are
  21. they blindingly fast, but they also take up very little memory. So when
  22. you're writing an arcade game, hardware sprites should always be at the
  23. top of your list.
  24.  
  25.   Each sprite is 16 pixews wise and up to 255 pixels high. The Amiga's
  26. hardware supports a maximum of eight three-colour sprites or four
  27. fifteen-colour sprites. Colour number zero is transparent - that's the
  28. reason for the odd colour ranges.
  29.  
  30.   At first glance, these features don't seem particulary impressive.
  31. But there are a couple of useful tricks which can increase both the
  32. numbers and sizes of these sprites beyond recognition.
  33.  
  34.   One solution is to take each hardware sprite and split it into a
  35. number of horizontal segments. These segments can be independently
  36. positioned, allowing you to apparently display dozens of sprites on the
  37. screen at once. Similarly, the width restriction can be exceeded by
  38. building an object out of several individual sprites. Using this
  39. technique it's easy to generate objects up to 128 pixels wide.
  40.  
  41.   Until recently the only way to exploit these techniques was to delve
  42. into the mysterious wolrd of 68000 assembler language. So you'll be
  43. delighted to discover that AMOS Basic manages the entire process
  44. automatically! Once you've designed your sprites with the AMOS sprite
  45. editor, you can effortlessly manipulate them with just a single Basic
  46. instruction.
  47.  
  48. The sprite commands
  49. ===================
  50. Remember to have a sprite bank loaded into memory when trying out the
  51. various commands in this chapter. We advise you use the file
  52. SPRITES.ABK from the AMOS data disc.
  53.  
  54.  
  55.  
  56.            SPRITE (display a hardware sprite on the screen)
  57.  
  58. SPRITE n,x,y,i
  59.  
  60. The SPRITE command displays a hardware sprite on the screen at
  61. coordinates x,y using image number i.
  62.  
  63.   n is the identification number of the sprite and can range from 0 to
  64. 63. Each sprite can be associated with a separate image from the sprite
  65. bank, so the same image can be used for several sprites.
  66.  
  67.   x and y hold the position of the sprite using special hardware           146
  68. coordinates. All measurements are taken from the *hot spot* of your
  69. images. This serves as a sort of 'handle' on the sprite and is used as
  70. a reference point for the coordinates. Normally the hot spot is set to
  71. the top left hand corner of an image. However it can be changed within
  72. your program using the HOT SPOT command.
  73.  
  74.   Hardware coordinates are independent of the screen mode and
  75. effectively start from (-129,-45) on the default screen. AMOS provides
  76. you with several built-in functions for conversions between hardware
  77. coordinates and the easier to use screen coordinates. See the X HARD,
  78. Y HARD, X SCREEN and Y SCREEN functions for more details.
  79.  
  80.   i is the number of a particular image stored in the sprite bank. This
  81. bank can be created using the AMOS sprite editor, and is automatically
  82. saved along with your Basic program. It can also be loaded directly
  83. with the LOAD instruction. In addition you can use the GET SPRITE
  84. command to grab an image straight off the current screen.
  85.  
  86.   Any of these parameters x,y and i may be optionally omitted, but the
  87. appropriate commas must be included. For example:
  88.  
  89.         Load "AMOS_DATA:Sprites/Octopus.abk"
  90.         Sprite 8,200,100,1
  91.         Sprite 8,,150,1
  92.         Sprite 8,300,,
  93.  
  94. For a demonstration of sprites in action, load EXAMPLE 11.1 from the
  95. MANUAL folder on the AMOS data disc.
  96.  
  97.  
  98. Computed sprites
  99. ================
  100. Although the Amiga only provides you with eight actual sprites, it's
  101. possible to use them to display up to 64 different objects on the
  102. screen at once. These objects are known as -computed sprites- and are
  103. managed antirely by AMOS Basic. Computed sprites can be assigned by
  104. calling the SPRITE command with a number greater than 7. For example,
  105.  
  106.         Load "AMOS_DATA:Sprites/Octopus.abk"
  107.         Sprite 8,200,100,1
  108.  
  109. The size of a computed sprite is taken directly from the image data,
  110. and can vary between 16 and 128 pixels wide, and from 1 to 255 pixels
  111. high.
  112.  
  113.   Before you can make full use of these sprites you need to understand
  114. some of the principles behind them. Each hardware sprite consists of a
  115. thin narrow strip 16 pixels wide and 256 pixels deep. Depending on the
  116. number of colours, you can have either eight or four of these strips on
  117. the screen at a time.
  118.  
  119.   It should be obvious that most of the area inside these sprites is       147
  120. effectively wasted. That's because few programs need sprites which are
  121. taller than about 40 or 64 pixels. However there is a simple trick
  122. which enables us to borrow this space to generate dozens of extra
  123. objects on the screen. Look at the picture AMOS1.PIC (included in this
  124. manual file packet) which contains the letters A,M,O and S.
  125.  
  126.            < picture   AMOS1.PIC >
  127.  
  128. This sprite can be split into four horizontal segments each enclosing a
  129. single letter. The Amiga's hardware allows each section to be freely
  130. positioned anywhere on the current line, making a total of four
  131. computed sprites. Here's a diagram which illustrates this process.
  132.  
  133.            < picture   AMOS2.PIC >                                         148
  134.  
  135. As you can see, a computed sprite is really just a small part of a
  136. hardware sprite displayed at a different horizontal screen position.
  137. Notice the line between each object. This is an unavoidable side effect
  138. of the repositioning process, and is generated by the Amiga's hardware.
  139.  
  140.   Due to the way computed sprites are produced, there are a couple of
  141. restrictions to their use. Firstly, you can't have more than 8 computed
  142. sprites on a single line. In practice the system is complicated by the
  143. need to produce sprites which are larger than the 16 pixel maximum.
  144. AMOS generates these objects by automatically positioning several
  145. computed sprites side by side. This can be seen from the diagram below:
  146.  
  147.            < picture   AMOS3.PIC >
  148.  
  149. The maximum of eight hardware sprites therefore imposes a strict limit
  150. to the number of such objects you can display on a horizontal line. The
  151. total width of the objects must not exceed:
  152.  
  153.         16*8=128 pixels for three-colour sprites                           149
  154.         16*4=64  pixels for fifteen-colour sprites
  155.  
  156. If you attempt to ignore limitation, you won't get an error message,
  157. but your computer sprite will not be displayed on the screen. So it's
  158. vital to ensure that the above restriction is never broken. This can be
  159. achieved using the following procedure:
  160.  
  161.   Add together the widths of all your computed sprites, multiplying the
  162. dimensios of any fifteen-colour sprites by two. If the total is
  163. greater than 128, you'll need to space your sprites on the screen so
  164. that their combined width lies below this value. Take particular care
  165. if you are animating your sprites with AMAL as certain combinations
  166. will only come to light after you've experimented with the sequence for
  167. some time. These problems will be manifested by the random
  168. disappearance of one or more sprites on the screen.
  169.  
  170.   If the worst comes to the worst, you'll need to substitute some of
  171. your larger sprites with Blitter Objects. This will increase the
  172. overall size of your program significantly, but it should have a
  173. negligible effect on the final quality of your game.
  174.  
  175.   These restrictions are not confined to AMOS Basic of course. They
  176. apply equally well to all games on the Amiga, even if they're written
  177. entirely in machine code! So there's nothing stopping you from
  178. producing your own Xenon II clone using exactly the same tehcniques.
  179.  
  180.   Note that, normally, hardware sprite number zero is allocated to the
  181. mouse cursor. You can release this sprite with a simple call to the
  182. HIDE command. See EXAMPLE 11.2.
  183.  
  184.  
  185. Creating an individual hardware sprite
  186. ======================================
  187. The only real problem with computed sprites is that you never know
  188. precisely which hardware sprite is going to be used in a particular
  189. object. Normally the hardware sprites used in an object will change
  190. whenever the object is moved. Occasionally this can be inconvenient,
  191. especially when you are animating objects such as missiles which need
  192. to remain visible in a wide range of possible sprite combinations.
  193.  
  194.   In these circumstances it's useful to be able to allocate a hardware
  195. sprite directly. Individual hardware sprites can be assigned using the
  196. SPRITE instruction with an identification number between 0 and 7.
  197. Example:
  198.  
  199.         Sprite 1,100,100,2
  200.  
  201. This loads a hardware sprite number 1 with image number 2. N now
  202. corresponds to the number of a single hardware sprite, and can range
  203. between 0 and 7. If your image is larger than sixteen pixels wide, AMOS
  204. will automatically grab the required sprites in consecutive order
  205. starting from the sprite you have chosen. For example:
  206.  
  207.         Sprite 2,200,100,1
  208.  
  209. Supposing image number 1 contained a 32-bit image with three colours.
  210. This command would allocate hardware spries 2 and 3 to the image.
  211. Nothing would happen if you were now to attempt to display hardware
  212. sprite 3 with a command like SPRITE 3,150,100,1  because this sprite
  213. has already been used. You would only have access to sprites 0,1,4,5,6
  214. and 7, and the maximum numbers and sizes of your computed sprites would
  215. be reduced accordingly.
  216.  
  217.   Each 15-colour sprite is implemented using a pair of two three-colour    150
  218. sprites. However, it's not possible to combinea ny two sprites in this
  219. way. Only the combinations 0/1,2/3,4/5,6/7 are allowed. One side effect
  220. of this, is that you should always assign your hardware sprites using
  221. even sprite numbers. Otherwise, AMOS will start your sprite from the
  222. next group of two, effectively wasting the first sprite.
  223.  
  224.   Also note that if you try to create a large fifteen-colour sprite
  225. with this system, you could easily use up all the available sprites in
  226. a single object.
  227.  
  228.   WARNING! If you are writing a screen scrolling game, you may
  229. encounter problems using sprites in conjunction with the SCREEN OFFSET
  230. and SCREEN DISPLAY commands. These generate a DMA clash between the
  231. sprite system and the screen bit-maps, and can occasionally lead to
  232. unwanted screen effects.
  233.  
  234.   This problem is only relevant if you are using hardware sprites 6/7.
  235. When the screen is shifted to the left with SCREEN OFFSET, the amount
  236. of time for your sprite updates is reduced, as the screen DMA has
  237. priority over the sprite system. Unfortunately, there isn't enough
  238. processing time to draw sprites 6/7, and they will therefore be
  239. corrupted on your display.
  240.  
  241.   To clear up this problem, create sprites 6/7 as individual hardware
  242. sprites and position them off the screen using negative coordinates.
  243. This will stop AMOS Basic from using them in your computed sprites.
  244. Providing sprites 6/7 are never displayed on the screen during your
  245. scrolling operations, all will be well.
  246.  
  247.  
  248. The sprite palette
  249. ==================
  250. The colours required by a hardware sprite are stored in the colour
  251. registers 16 to 31. Providing your current screen mode doesn't make use
  252. of these registers, the sprite colours will be completely separate from
  253. your screen colours. Interestingly enough, this is also the case for
  254. the 4096-colour Ham mode. So there's nothing stopping you from
  255. producing some mind-blowing Ham games with this system!
  256.  
  257.   However you will encounter real problems when using 32 or 64 colour
  258. screen in conjunction with three colour sprites. This is because the
  259. colours used by these sprites are grouped together in the following
  260. way:
  261.            Hardware sprites  Colour registers
  262.            ----------------  ----------------
  263.                  0 / 1         17 / 18 / 19
  264.                  2 / 3         21 / 22 / 23
  265.                  4 / 5         25 / 26 / 27
  266.                  6 / 7         29 / 30 / 31
  267.  
  268.    Colour registers 16,20,24 and 28 are treated as transparent.
  269.  
  270. The difficulty arises due to the way AMOS generates computed sprites.
  271. The hardware sprites used to produce these objects vary during the
  272. course of a game, so it's vital to ensure that the three colours used
  273. by each individual sprite are set to exactly the same values, otherwise
  274. the colours of your computed sprites will change unpredictably. Here's
  275. a small AMOS procedure which will perform the entire process for you       151
  276. automatically.
  277.  
  278.         Procedure INIT_SPRITES
  279.           Get Sprite Palette
  280.           For S=0 To 3
  281.             For C=0 To 2
  282.               Colour S*4+C+17,Colour(C)
  283.             Next C
  284.           Next S
  285.         Endproc
  286.  
  287. The above restriction does not, of course, apply to fifteen-colour
  288. sprites. If you want to make the most of the Extra Half Bright or
  289. 32-colour modes, you may find it easier to avoid using four-colour
  290. sprites altogether.
  291.  
  292.  
  293.  
  294.                     GET SPRITE PALETTE (grab sprite
  295.                          colours into screen)
  296.  
  297. GET SPRITE PALETTE [mask]
  298.  
  299. This loads the entire colour palette used for your sprite images into
  300. the current screen. The optional "mask" allows you to load just a
  301. selection of the colours from the sprite palette. Each of the 32
  302. colours is represented by a single bit in the mask, numbered from right
  303. to left. The rightmost bit represents the status of colour zero, the
  304. next vit colour 1, and so on. To load a colour simply set the
  305. appropriate bit to 1. If, for instance, you wanted to copy just the
  306. first four colours, you would set the bit pattern to:
  307.  
  308.         Get Sprite Palette %0000000000001111
  309.  
  310. Identically, since bobs use the same sprite bank as sprites, this
  311. command can also be used to load the colours of a bob.
  312.  
  313.  
  314. Controlling sprites
  315. ===================
  316.  
  317.  
  318.                  SET SPRITE BUFFER (set height of the
  319.                            hardware sprites)
  320.  
  321. SET SPRITE BUFFER n
  322.  
  323. This sets the work area in which AMOS creates the images of the
  324. hardware sprits. Acceptable values for n range from 16 to 256. TO set
  325. the correct value for n, simply examine the sprites in the sprite
  326. editor and work out which is the largest sprite length wise. ANy sprite
  327. that is larger than "n" will simply be truncated at the appropriate cut
  328. off point.
  329.  
  330.   SET SPRITE BUFFER is supplied for your use so that you can claim back
  331. any redundant memory our game or application simply doesn't use.
  332.  
  333.   The amount of memory consumed by the sprite buffer can be calculated
  334. using the formula:
  335.  
  336.         Memory = N*4*8*3 = N*96
  337.  
  338.   So the minimum buffer size is 1536 bytes and the maximum is 24k.
  339. Note: This command erases all current sprite assignments and resets the
  340. mouse cursor to its original state.
  341.  
  342.  
  343.  
  344.                     SPRITE OFF (remove one or more                         152
  345.                        sprites from the screen)
  346. SPRITE OFF [n]
  347.  
  348. The SPRITE OFF command removes one or more sprites from the screen. All
  349. current sprite movements are aborted. In order to restart them, you'll
  350. need to completely reinitialize your movement pattern.
  351.  
  352. SPRITE OFF    Removes all the sprites from display
  353.  
  354. SPRITE OFF n  Only deactivates sprite number n
  355.  
  356. Note that your sprites are automatically deactivated whenever you call
  357. up the AMOS Basic editor. They will be automatically returned to their
  358. original positions the next time you enter direct mode.
  359.  
  360.  
  361.  
  362.                SPRITE UPDATE (control sprite movements)
  363.  
  364. SPRITE UPDATE [ON/OFF]
  365.  
  366. The SPRITE UPDATE command provides you with total control of the
  367. movements of your sprites. Normally, whenever you move a sprite, its
  368. position is updated automatically during the next vertical blank period
  369. (see WAIT VBL). But if you are moving a lot of sprites using the SPRITE
  370. command, the updates will occur before all the sprites have been moved.
  371. This may result in a noticeable jump in yur movement patterns. In these
  372. circumstances, you can turn off the automatic updating system with the
  373. SPRITE UPDATE OFF command.
  374.  
  375.   Once your sprites have been succesfully moved, you can then slide
  376. them smoothly into place with a call to SPRITE UPDATE. This will
  377. reposition any sprites which have moved since your last update.
  378.  
  379.  
  380.  
  381.                =X SPRITE (get x coordinate of a sprite)
  382.  
  383. x=X SPRITE(n)
  384.  
  385. Returns the current x coordinate of sprite n, measured the hardware
  386. system. This command allows you to quickly check whether a sprite has
  387. passed of the edge of the Amiga's screen.
  388.  
  389.  
  390.  
  391.                =Y SPRITE (get y coordinate of a sprite)                    153
  392.  
  393. y=Y SPRITE(n)
  394.  
  395. Y SPRITE returns a sprite's vertical position. As usual, n refers to
  396. the number of the sprite and can range from 0 to 63. Remember, all
  397. sprite positions are measured in hardware coordinates. See EXAMPLE 11.3
  398.  
  399.  
  400.  
  401.                GET SPRITE (load a section of the screen
  402.                          into the sprite bank)
  403.  
  404. GET SPRITE [s,] i,x1,y1 TO x2,y2
  405.  
  406. This instruction enables you to grab images directly off the screen and
  407. turn them into sprites. The coordinates x1,y1 and x2,y2 define a
  408. rectangular area to be captured into the sprite bank. Normally all
  409. images are taken from the current screen. However it's also possible to
  410. grab the image from a specific screen using the optional screen number
  411. "s".
  412.  
  413.   Note: There are no limitations to the region that may be grabbed in
  414. this way. Providing your coordinates lie inside the existing screen
  415. borders, everything will be fine.
  416.  
  417.   i denotes the number of the new image. If there is no existing sprite
  418. with this number, a new image will be created automatically. AMOS wlil
  419. also take the trouble of reserving the sprite bank if it hasn't been
  420. previously defined. See EXAMPLE 11.4
  421.  
  422.   There's also an equivalent GET BOB instruction which is identical to
  423. GET SPRITE in every respect. Since the sprite bank is shared by both
  424. bobs and sprites, the images are in exactly the same format. So it's
  425. perfectly acceptable to use both instructions in conjunction with
  426. either bobs or sprites. Try changing the sprite instruction in the
  427. previous example to something like:
  428.  
  429.         Bob 1,0,0,1
  430.  
  431.  
  432. Conversion functions
  433. ====================
  434.  
  435.  
  436.                  =X SCREEN (convert hardware coordinates
  437.                  =Y SCREEN  into screen coordinates)
  438.  
  439. x=X SCREEN([n,] xcoord)
  440. y=Y SCREEN([n,] ycoord)
  441.  
  442. Transforms a hardware coordinate into a screen cordinate relative to
  443. the current screen. If the hardware coordinates lie outside the screen
  444. then both functions will return relative offsets from the screens
  445. boundaries. Type the following from direct mode:
  446.  
  447.         Print X Screen(130)
  448.  
  449. The result will be -2. This is because the x screen coordinate 0 is
  450. equal to hardware coordinate 128 and thus the conversion of 130 to a
  451. screen coordinate results in a position two pixels to the left of the
  452. screen.
  453.  
  454.   If the optional screen number is included then the coordinates will
  455. be returned relative to screen # n.
  456.  
  457.  
  458.  
  459.                   =X HARD (convert screen coordinates                      154
  460.                   =Y HARD  into hardware coordinates)
  461.  
  462. X=X HARD ([n,] xcoord)
  463.  
  464. These functions convert a screen coordinate into a hardware coordinate.
  465. There are four separate conversion functions, the above syntaz converts
  466. xcoord from a coordinate relative to the current screen to a hardware
  467. coordinate.
  468.  
  469. Y=Y HARD ([n,] ycoord)
  470.  
  471. Transforms a Y coordinate relative to the current screen into hardware
  472. coordinate. As before, n specififes a screen number for use with the
  473. functions. All coordinates will now be returned relative to this
  474. screen.
  475.  
  476.  
  477.  
  478.              =I SPRITE (return current image of a sprite)
  479.  
  480. Image=I SPRITE(n)
  481.  
  482. This function returns the current image number being used by sprite n.
  483. A value of zero will be reported if the sprite is not displayed.
  484.  
  485.  
  486.  
  487.  
  488.  
  489.  
  490.